<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/4/19
 * Time: 18:09
 * 营收管理
 */
namespace app\admin\controller;
use think\Db;
class Revenue extends Base {

    public function index(){
        if(request()->isAjax()){
            $params = input('param.');
            $where = array();
            if(isset($params['revenue_mode']) && $params['revenue_mode'] != '')
            {
                $where['a.revenue_mode'] = $params['revenue_mode'];
            }
            if(isset($params['key']) && $params['key'] != '')
            {
                $where['s_name'] = ['like', "%" . $params['key'] . "%"];
            }
//            $where['a.task_status'] = array('NEQ',5);
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('revenue')->alias('a')
                ->join('up_shops us','us.s_id = a.revenue_shop_id','left')
                ->where($where)->count();// 获取总条数
            $lists = Db::name('revenue')->alias('a')
                ->join('up_shops us','us.s_id = a.revenue_shop_id','left')
                ->where($where)
                ->page($Nowpage,$limits)
                ->field('a.*,us.s_name')
                ->select();
            foreach ($lists as $key=>&$vo){
                if($vo['revenue_admin_id']!=''){
                    $username = Db::table('up_admin')->where('id',$vo['revenue_admin_id'])->value('username');
                    $vo['admin_name']=$username;
                }else{
                    $vo['admin_name']='';
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    //上传凭证
    public function img(){
        if(request()->isPost()){
            $img = input('param.lunbo_image');
            $id = input('param.id');
            $data['revenue_cz_time'] = date('Y-m-d H:i:s',time());
            $data['revenue_admin_id'] = session('uid');
            if($img!=''){
                $data['revenue_file_name'] = $img;
            }
            $res = Db::table('up_revenue')->where('revenue_id',$id)->update($data);
             return json(['code' => 200, 'msg' => '操作成功']);
        }
        $id = input('param.id');
        $info = Db::name('revenue')->where('revenue_id',$id)->find();
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function excelIn(){
        $ids = input('param.id');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择数据']);
        }
        $where['a.revenue_id'] = $ids;
        $lists = Db::name('revenue')->alias('a')
            ->join('up_shops us','us.s_id = a.revenue_shop_id','left')
            ->where($where)
            ->field('a.revenue_number,a.revenue_time,a.revenue_type,a.revenue_money,
            revenue_mode,us.s_name,a.revenue_file,a.revenue_file_name,a.revenue_cz_time,a.revenue_admin_id')
            ->find();
        if($lists['revenue_admin_id']!=''){
            $username = Db::table('up_admin')->where('id',$lists['revenue_admin_id'])->value('username');
            $lists['admin_name']=$username;
        }else{
            $lists['admin_name']='';
        }
        if($lists['revenue_type']==1){
            $lists['revenue_type_name'] = '企业充值';
        }elseif ($lists['revenue_type']==2){
            $lists['revenue_type_name'] = '达人介绍';
        }

        if($lists['revenue_file']==2){
            $lists['revenue_file_name_s'] = '上传了';
        }else{
            $lists['revenue_file_name_s'] = '没有上传';
        }
        if($lists['revenue_mode']==1){
            $lists['revenue_mode_name'] = '微信支付';
        }elseif ($lists['revenue_mode']==2){
            $lists['revenue_mode_name'] = '支付宝支付';
        }elseif ($lists['revenue_mode']==3){
            $lists['revenue_mode_name'] = '平台扣除';
        }elseif ($lists['revenue_mode']==4){
            $lists['revenue_mode_name'] = '平台充值';
        }elseif ($lists['revenue_mode']==5){
            $lists['revenue_mode_name'] = '配置达人';
        }
        $res = Excel('营收管理列表','营收管理列表',$lists);
        return json($res);
    }


}